﻿using System.Xml.Linq;

namespace EasyXaf.DynamicForm.Models;

public class Validation : Element
{
    public string Rule { get; set; }

    public string Message { get; set; }

    public Validation(Element parent)
        : base(parent)
    {
    }

    public override void WriteXml(XElement element)
    {
        if (!string.IsNullOrWhiteSpace(Rule))
        {
            SetXmlAttributeValue(element, "rule", Rule);
        }

        if (!string.IsNullOrWhiteSpace(Message))
        {
            SetXmlAttributeValue(element, "message", Message);
        }
    }

    public override void ReadXml(XElement element)
    {
        Rule = GetXmlAttributeValue<string>(element, "rule");
        Message = GetXmlAttributeValue<string>(element, "message");
    }

    public override XElement CreateXElement()
    {
        return new XElement("validation");
    }
}
